<?php
namespace app\api\controller;

use think\Controller;
use think\Request;
use app\admin\model\Demand as Demands;

class Demand extends Controller
{
    public function insertDemands(Request $request){
        if($request->post()){
            $demands = new Demands();
            $data = $request->post();
            //先查询今日是否提交过1次
            $getWhere['user_id'] = $data['user_id'];
            $theDay = mktime(0,0,0,date('m'),date('d'),date('Y'));
            $getWhere['ctime'] = ['gt',$theDay];
            $getUser = $demands->getOne($getWhere);
            if($getUser){
                //已提交过1次
                $res['code'] = 0;
                $res['msg'] = '今日已提交信息';
            }else{
                //未提交过
                $data['ctime'] = time();
                $rs = $demands->add($data);
                if($rs){
                    $res['code'] = 1;
                    $res['msg'] = '提交成功';
                }else{
                    $res['code'] = 0;
                    $res['msg'] = '提交失败';
                }
            }
        }else{
            $res['code'] = 0;
            $res['msg'] = '非法提交';
        }
        echo json_encode($res);
    }
}
